package com.stylefeng.guns.core.util;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: Huang Yuehong
 * @Description: 文件上传工具类
 * @Date: 2018/7/4 14:24
 */
public class FileUploadUtil {

    /**
     * 文件上传处理
     *
     * @param file
     * @return
     */
    public static Map writeUploadFile(MultipartFile file, String module) throws IOException {

        String filename = file.getOriginalFilename();
        String realpath = "D:/fpmsaProject/" +  module +"/";

        File fileDir = new File(realpath);

        // 文件夹不存在则创建
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }

        String newName = new Date().getTime() +"_"+filename;
        File tempFile = new File(fileDir.getPath()+File.separator+newName);
        file.transferTo(tempFile);


        Map<String, String> result = new HashMap<>();
        result.put("filename", filename);
        result.put("realName", tempFile.getName());
        result.put("relatPath", realpath+File.separator+newName);
        return result;
    }
}

